package com.finalhomework.dms.controller;


import com.finalhomework.dms.dao.DorRepository;
import com.finalhomework.dms.dao.StudentRepository;
import com.finalhomework.dms.entities.Students;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    StudentRepository studentRepository;
    @Autowired
    DorRepository dorRepository;
    @RequestMapping("stu_dormitory")
    public String skipStu_dor(Model model, HttpServletRequest request){
        Object loginUser=request.getSession().getAttribute("loginUser");
        Students studentsByNumber = studentRepository.getStudentsByNumber(loginUser);
        List<Students> all = studentRepository.findAll();
        List<Students> ByNo=new ArrayList<>();
        for (Students students: all){
            if (students.getDormitory().getDormitoryNo().equals(studentsByNumber.getDormitory().getDormitoryNo())){
                ByNo.add(students);
            }
        }
        model.addAttribute("byNo",ByNo);

        return "user/stu_dormitory";
    }
    @RequestMapping("/personal")
    public String skipPersonal(Model model, HttpServletRequest request){
        Object loginUser=request.getSession().getAttribute("loginUser");
        Students studentsByNumber = studentRepository.getStudentsByNumber(loginUser);
        model.addAttribute("stu",studentsByNumber);

        return "user/personal";
    }
    @RequestMapping("/updatePsw")
    public String skipPsw(Model model,String msg){
        if(msg!=null){
            switch (msg) {
                case "msg1" -> model.addAttribute("msg1", "原密码错误，请重新输入");
                case "msg2" -> model.addAttribute("msg2", "新密码与确认密码不相同，新重新输入");
                case "msg3" -> model.addAttribute("msg3", "新密码与原密码相同，新重新输入");
                case "msg4" -> model.addAttribute("msg4", "修改成功");
            }
        }


        return "user/updatePsw";
    }

    @PostMapping("/students/changePsw")
    public String changePsw(RedirectAttributes msg,
                            @RequestParam(value = "password") String psw,
                            @RequestParam(value = "new_psw") String new_psw,
                            @RequestParam(value = "confirm_psw")String confirm_psw,
                            HttpServletRequest request){
        Object loginUser=request.getSession().getAttribute("loginUser");
        Students students = studentRepository.getStudentsByNumber(loginUser);
        System.out.println(psw+new_psw+confirm_psw);
        if (!students.getPaw().equals(psw)){
            msg.addAttribute("msg","msg1");
            return "redirect:/updatePsw";
        }else if(!new_psw.equals(confirm_psw)){
            msg.addAttribute("msg","msg2");
            return "redirect:/updatePsw";
        }else if (new_psw.equals(psw)){
            msg.addAttribute("msg","msg3");
            return "redirect:/updatePsw";
        }
        students.setPaw(new_psw);
        studentRepository.save(students);
        msg.addAttribute("msg","msg4");
        return "redirect:/updatePsw";
    }



}

